<?php 
	ini_set('max_execution_time', 3600);
    error_reporting(E_ALL);
    require_once('../config/constant.php');
	require_once(DAEMONE_HOME.'/config/database.php');

	$date = new DateTime(); 
	$date->setTimezone(new DateTimeZone('Asia/Karachi'));
	$table_date_postfix = date_format($date, "Y_m_d_H_i");
	$rss_feed_file_name_prefix = "rss_feed_item_${table_date_postfix}";
	$data_folder_path = DAEMONE_HOME."/scripts/../../articles_data";
	
	$mysql_command = 'mysql -h'.$config['db']['host'].' -u'.$config['db']['username'].' -p'.$config['db']['password'] . ' '.$config['db']['database'];
	$mysqldump_command = 'mysqldump -h'.$config['db']['host'].' -u'.$config['db']['username'].' -p'.$config['db']['password'] . ' '.$config['db']['database'];
	
	$command_1 = "echo drop table if exists rss_feed_item_${table_date_postfix} | ${mysql_command}";
	$command_2 = "echo create table rss_feed_item_${table_date_postfix} select * from rss_feed_item where grabbed_status = 2  | ${mysql_command} ";
	$command_3 = "$mysqldump_command rss_feed_item_${table_date_postfix} >  ${data_folder_path}/${rss_feed_file_name_prefix}.sql" ;
	$command_4 = "zip -r ${data_folder_path}/${rss_feed_file_name_prefix}.zip  ${data_folder_path}/${rss_feed_file_name_prefix}.sql" ;
	$command_5 = "echo update rss_feed_item set grabbed_status = 3 where grabbed_status = 2 | ${mysql_command}";

	echo 'executing command ... ', $command_1, " |<br/>\r\n";
	echo shell_exec($command_1);

	echo 'executing command ... ', $command_2, " |<br/>\r\n";
	echo shell_exec($command_2);

	echo 'executing command ... ', $command_3, " |<br/>\r\n";
	echo shell_exec($command_3); 
	
	echo 'executing command ... ', $command_4, " |<br/>\r\n";
	echo shell_exec($command_4); 
		
	$ftp_server = 'articlepages.co.cc';
	$ftp_user_name = 'freearticleboxcom';
	$ftp_password = 'Str0ng!@#$';
	$ftp_source_file_path = "${data_folder_path}/${rss_feed_file_name_prefix}.zip";
	$ftp_distination_file_path = "/articlepages.co.cc-redirect/articles_data/${rss_feed_file_name_prefix}.zip";
	
	// set up basic connection
	$conn_id = ftp_connect('articlepages.co.cc'); 

	// login with username and password
	$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_password); 

	// check connection
	if ((!$conn_id) || (!$login_result)) 
	{ 
		echo "FTP connection has failed! <br/>\r\n";
		exit; 
	} 
	else 
	{
		// upload the file
		echo "Connected to $ftp_server, for user $ftp_user_name <br/>\r\n";
		echo "uploading file  $ftp_source_file_path to ${ftp_user_name}@${ftp_server}:${ftp_distination_file_path} ... start <br/>\r\n";
		
		$upload = ftp_put($conn_id, $ftp_distination_file_path, $ftp_source_file_path, FTP_BINARY); 

		// check upload status
		if (!$upload) { 
			echo "FTP upload has failed!";
		} else {
			echo "uploading file  $ftp_source_file_path to ${ftp_user_name}@${ftp_server}:${ftp_distination_file_path} ... done <br/>\r\n";
			
			echo 'executing command ... ', $command_5, " |<br/>\r\n";
			echo shell_exec($command_5); 
		}

		
	}

	// close the FTP stream 
	ftp_close($conn_id); 
	
?>

